package com.qiu.greened.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiu.greened.mapper.FollowMapper;
import com.qiu.greened.pojo.Follow;
import com.qiu.greened.service.IFollowService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 关注表 服务实现类
 * </p>
 *
 * @author qiu
 * @since 2021-12-09
 */
@Service
@AllArgsConstructor
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    private final FollowMapper followMapper;


    @Override
    public String addOrDeletePostFollow(Follow follow) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", follow.getUserId()).eq("follow_user_id", follow.getFollowUserId());
        Follow one = followMapper.selectOne(queryWrapper);
        String msg;
        if (one == null) {
            followMapper.insert(follow);
            msg = "insert";
        } else {
            followMapper.delete(queryWrapper);
            msg = "delete";
        }
        return msg;
    }

    @Override
    public Boolean selUserPostFollow(int userId, int followUserId) {
        boolean follow = false;
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("follow_user_id", followUserId);
        if (followMapper.selectOne(queryWrapper) != null) {
            follow = true;
        }
        return follow;
    }
}
